#include<iostream>
using namespace std;
#include<vector>
#include<map>
class Solution {
public:
    long long numberOfPairs(vector<int>& nums1, vector<int>& nums2, int k) {
        map<int, int> count1, count2;
        int last=0;
        long long ans = 0;
        for (auto& e : nums1) {
            ++count1[e];
            last = max(last, e);
        }
        for (auto& e : nums2)
            ++count2[e];
        for (auto& e : count2) {
            int a1 = e.first, a2 = e.second;
            int now = a1 * k;
            for (int i = now; i <= last; i += now) {
                if (count1.count(i))
                    ans += count1[i] * a2;
            }
        }
        return ans;
    }
};